package com.lunwen.view;

import com.lunwen.dao.StudentDao;
import com.lunwen.domain.Student;
import com.lunwen.utils.StringUtils;
import com.lunwen.view.component.Context;
import com.lunwen.view.component.MassageTip;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudentFormFrame extends Context implements ActionListener {

    public JTextField sid = new JTextField(10);
    public JTextField name = new JTextField(10);
    public JTextField major = new JTextField(10);
    public JTextField clazz = new JTextField(10);
    public JTextField professor = new JTextField(10);
    public JTextField phone = new JTextField(10);

    private JButton add = new JButton("添加学生信息");

    private StudentDao dao = new StudentDao();

    public StudentFormFrame(int width, int height) {
        super(width, height);

        sid.setBounds(60,0,width-120,40);
        name.setBounds(60,50,width-120,40);
        major.setBounds(60,100,width-120,40);
        clazz.setBounds(60,150,width-120,40);
        professor.setBounds(60,200,width-120,40);
        phone.setBounds(60,250,width-120,40);
        this.add(sid, "学生学号", 100);
        this.add(name, "学生姓名", 100);
        this.add(major, "专业名称", 100);
        this.add(clazz, "所在班级", 100);
        this.add(professor, "导师姓名", 100);
        this.add(phone, "学生联系方式", 150);

        add.setBounds(60,300,width-120,40);
        this.add(add,20);
        add.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == add){
            if (!StringUtils.isEmpty(sid.getText()) &&
                    !StringUtils.isEmpty(name.getText()) &&
                    !StringUtils.isEmpty(major.getText()) &&
                    !StringUtils.isEmpty(clazz.getText())){

                Student student = new Student(
                        sid.getText(),
                        name.getText(),
                        major.getText(),
                        clazz.getText(),
                        professor.getText(),
                        phone.getText()
                );
                int result = dao.insert(student);
                if (result > 0){
                    MassageTip.showMassage(null,"学生添加成功");
                }else{
                    MassageTip.showMassage(null,"学生添加失败");
                }
            }else{
                MassageTip.showMassage(null,"字段不可为空");
            }
        }
    }
}
